<?php
class Database{
	const USERNAME = 'root';
	const PASSWORD = '';
	const iPLocalHost = 'localhost';
	const dbName = 'ticketing';
	protected $db;
	
	private $error;

	public function __construct(){
		$error = false;
		$this->db = @new mysqli(Database::iPLocalHost, Database::USERNAME, Database::PASSWORD, Database::dbName) or $error = true;
	}

	public function __destruct(){
		@$this->db->close();
	}

	protected function getError(){
		return $this->db->connect_error;
	}
	
	public function getDb() {
		return $this->db;
	}
	
	protected function startTransaction() {
		try {
			$query_autocommit = $this->db->query("SET AUTOCOMMIT=0");
			$query_start = $this->db->query("START TRANSACTION");
			if($query_autocommit and $query_start) {
				return true;
			} else {
				throw new Exception('startTransaction');
			}
		} catch(Exception $e) {
			Logging::errLogString('Exception', $e->getMessage());
			return false;
		}
	}
	
	protected function stopTransaction($dbError) {
		try {
			if($dbError) {
				$this->db->query("ROLLBACK");
				$this->db->query("SET AUTOCOMMIT=1");
				return false;
			} else {
				$this->db->query("COMMIT");
				$this->db->query("SET AUTOCOMMIT=1");
				return true;
			}
		} catch(Exception $e) {
			echo $e;
			Logging::errLogString('Exception', 'stopTransaction');
			return false;
		}
	}
}
?>